package com.tigase.serviceImpl;

import com.tigase.bean.TigaseMessage;
import com.tigase.service.MessageService;
import com.tigase.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.*;

/**
 * Created by qt2 on 2017/7/21.
 */
@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public int insertMessage(TigaseMessage tigaseMessage) {
        String sql = "insert into t_message(message_id,message_from,message_to,message_type,message_body,picture_url,file_url,file_type,send_time) values(?,?,?,?,?,?,?,?,?)";
        String uuid= UUIDUtils.getUUID();
        int result = jdbcTemplate.update(sql,new Object[]{uuid,tigaseMessage.getMessageFrom(),tigaseMessage.getMessageTo(),tigaseMessage.getMessageType(),tigaseMessage.getMessageBody(),
        tigaseMessage.getPictureUrl(),tigaseMessage.getFileUrl(),tigaseMessage.getFileType(),new Date()});
        return result;
    }

    @Override
    public List<TigaseMessage> getAll(String messageFrom, String messageTo) {
        List<TigaseMessage> messages = new ArrayList<>();

        String sql = "select t.message_from,t.message_to,t.message_type,t.message_body,t.picture_url,t.file_url,t.file_type,t.send_time " +
                "  from t_message t  where t.message_from in (?,?) and t.message_to in (?,?) ORDER BY t.send_time DESC ";

        List<Map<String ,Object>> list = jdbcTemplate.queryForList(sql,new Object[]{messageFrom,messageTo,messageFrom,messageTo});

        if(list.size()>0){
            for(int i=0;i<list.size();i++){
                TigaseMessage tigaseMessage= new TigaseMessage();
                String from =(String) list.get(i).get("message_from");
                String to = (String) list.get(i).get("message_to");
                String type = (String) list.get(i).get("message_type");
                String body = (String) list.get(i).get("message_body");
                String picUrl = (String) list.get(i).get("picture_url");
                String fileUrl = (String) list.get(i).get("file_url");
                String fileType = (String) list.get(i).get("file_type");
                Timestamp time =(Timestamp) list.get(i).get("send_time");

                tigaseMessage.setMessageFrom(from);
                tigaseMessage.setMessageTo(to);
                tigaseMessage.setMessageType(type);
                tigaseMessage.setMessageBody(body);
                tigaseMessage.setPictureUrl(picUrl);
                tigaseMessage.setFileUrl(fileUrl);
                tigaseMessage.setFileType(fileType);
                tigaseMessage.setSendTime(time);

                messages.add(tigaseMessage);
             }
        }
        return messages;
    }

    @Override
    public int deleteMessage(Timestamp sendTime) {
    //    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    //    long ts = sendTime.getTime();
    //    String  res = String.valueOf(ts);
    //    System.out.println(res+"111111111111111111111");
        String sql = "delete t from t_message t where t.send_time = ? ";
        int result = jdbcTemplate.update(sql,new Object[]{sendTime});
        return result;
    }

    @Override
    public int moveMessage(Timestamp nowTime) {
        //时间大于7天为消息失效
        Long  Time = nowTime.getTime() - (7*24*3600000);
        Timestamp sendTime =  new Timestamp(Time);
        System.out.println(sendTime+"00000000000000");
        String sql = "delete t from t_message t where t.send_time < ? ";
        int result = jdbcTemplate.update(sql,new Object[]{sendTime});
        return  result;
    }
}
